//
//  ReceiveCoinViewController.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/5/5.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class ReceiveCoinViewController: BaseViewController {
    var viewModel:GoogleViewModel?
    lazy var selectBtn:CustomBtn = {
        let btn = CustomBtn()
        btn.nameLabel.font = UIFont.systemFont(ofSize: 16)
        btn.addTarget(self, action: #selector(selectAction), for: UIControlEvents.touchUpInside)
        return btn
    }()
    
    lazy var receiveCoinView:ReceiveCoinView = {
        let view = ReceiveCoinView(dataSource: viewModel)
        return view
    }()
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        viewModel = GoogleViewModel(controller: self)
        viewModel?.googleBool = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.addSubview(receiveCoinView)
        receiveCoinView.snp.makeConstraints { (make) in
            make.top.left.bottom.right.equalToSuperview()
        }
        
        self.navigationItem.titleView = selectBtn
        selectBtn.photoImageView.image = UIImage(named: "arrow_down")
        selectBtn.photoImageView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview()
        }
        selectBtn.nameLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview()
        }
        if viewModel?.selectModel.coinCode == nil {
            viewModel?.archiveSelectModel(selectIndex: 0)
        }
        viewModel?.archiveSlectName()
        var name = viewModel?.selectModel.coinName ?? ""
        if AppCacheInfo.default.isEnlish {
            name = " " + (viewModel?.selectModel.coinFullName ?? "")
        }
        selectBtn.nameLabel.text = String(format: "%@%@(%@)", kText("接收"),name,viewModel?.selectModel.coinCode ?? "")
        receiveCoinView.nameLabel.text = String(format: "%@%@", kText("向如下地址发送"),name)
        receiveCoinView.titleLabel.text = String(format: kText("请确保向该地址发送的是 %@(%@)，否则资产将会丢失且无法找回。"), name,viewModel?.selectModel.coinCode ?? "")
        let size = selectBtn.nameLabel.sizeThatFits(CGSize(width: kSCREEN_WIDTH-100, height: 44))
        selectBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
            make.width.equalTo(size.width + 20)
        }
        self.navigationItem.titleView?.layoutIfNeeded()
        
        self.viewModel?.archiveCoinInformation()
    }
    
    @objc func selectAction() {
        SelectActionManger.manager.creatSheet(style: .commonSty, titleArr: (viewModel?.coinNameArr)!, selectStr: viewModel?.selectModel.coinCode != nil ? viewModel?.selectModel.coinCode : "", dataSource: self)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension ReceiveCoinViewController:SelectmangerDelegate {
    func selectModel(model: SheetModel) {
        if model.selectStr != nil {
            if viewModel?.selectModel.coinCode == model.selectStr {
                return
            }
            receiveCoinView.addressLabel.text = ""
            receiveCoinView.qrCodeImageView.image = nil
            let index = viewModel?.coinNameArr.index(of: model.selectStr!)
            viewModel?.archiveSelectModel(selectIndex: index!)
            
            var name = viewModel?.selectModel.coinName ?? ""
            if AppCacheInfo.default.isEnlish {
                name = " " + (viewModel?.selectModel.coinFullName ?? "")
            }
            
            selectBtn.nameLabel.text = String(format: "%@%@(%@)",kText("接收"),name, viewModel?.selectModel.coinCode ?? "")
            let size = selectBtn.nameLabel.sizeThatFits(CGSize(width: kSCREEN_WIDTH-100, height: 44))
            selectBtn.snp.updateConstraints { (make) in
                make.width.equalTo(size.width + 20)
            }
            self.viewModel?.archiveCoinInformation()
            
            receiveCoinView.nameLabel.text = String(format: "%@%@", kText("向如下地址发送"),viewModel?.selectModel.coinName?.count != nil ? (viewModel?.selectModel.coinName)! : "")
            receiveCoinView.titleLabel.text = String(format: kText("请确保向该地址发送的是 %@(%@)，否则资产将会丢失且无法找回。"), name,viewModel?.selectModel.coinCode ?? "")
        }
    }
    
    
    
}
